package com.fowo.api.controller.purchase;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.fowo.api.common.model.R;
import com.fowo.api.entity.PurchaseOrder;
import com.fowo.api.entity.PurchaseOrderProduct;
import com.fowo.api.model.purchase.order.PurchaseOrderSearchParamPo;
import com.fowo.api.model.purchase.rgoods.order.PurchaseRgoodsOrderVo;
import com.fowo.api.service.PurchaseRgoodsOrderService;
import com.fowo.api.service.impl.PurchaseOrderProductServiceImpl;
import com.fowo.api.service.impl.PurchaseOrderServiceImpl;
import com.fowo.api.user.model.JwtUserInfo;
import io.swagger.v3.oas.annotations.Operation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@RequestMapping("/api/purchase-rgoods-Dorder")
@RestController
public class PurchaseRgoodsDOrderController {

    @Resource
    private PurchaseRgoodsOrderService service;
    @Resource
    private PurchaseOrderServiceImpl purchaseOrderService;
    @Resource
    protected PurchaseOrderProductServiceImpl purchaseOrderProductService;

    @Operation(summary = "获取采购订单")
    @GetMapping("getPurchaseOrder")
    public R getPurchaseOrder(PurchaseOrder purchaseOrder) {
        List<PurchaseOrder> list = purchaseOrderService.list(Wrappers.<PurchaseOrder>query().lambda()
                .eq(PurchaseOrder::getSupplierId, purchaseOrder.getSupplierId())
                .eq(PurchaseOrder::getWid, purchaseOrder.getWid())
        );
        return R.data(list);
    }

    @Operation(summary = "获取采购订单子表信息")
    @GetMapping("getPurchaseOrderItem")
    public R getPurchaseOrderItem(PurchaseOrder purchaseOrder) {
        PurchaseOrder one = purchaseOrderService.getOne(Wrappers.<PurchaseOrder>query().lambda().eq(PurchaseOrder::getPurchaseOrderCode, purchaseOrder.getPurchaseOrderCode()));
        List<PurchaseOrderProduct> list = purchaseOrderProductService.list(Wrappers.<PurchaseOrderProduct>query().lambda().eq(PurchaseOrderProduct::getPurchaseOrderId, one.getId()));
        return R.data(list);
    }
    //保存订单

    @PreAuthorize("@am.canAccess('purchaseRgoodsOrderCreate')")
    @Operation(summary = "新增")
    @PostMapping
    public R<Long> create(@Validated @RequestBody PurchaseRgoodsOrderVo model) {
        return R.data(() -> service.create(model));
    }




}
